package cn.walls1717.wallsmall.seckill.controller;

import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.seckill.pojo.to.SeckillSkuRedisTO;
import cn.walls1717.wallsmall.seckill.service.SeckillService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author walls1717
 * @since 2022/11/26 15:36
 **/
@Controller
public class SecKillController {
    @Resource
    private SeckillService seckillService;

    /** 返回当前时间可以参与的秒杀商品信息 */
    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTO> SeckillSkuRedisTO = seckillService.listCurrentSeckillSkus();
        return R.ok().put("data", SeckillSkuRedisTO);
    }

    /** 查询指定skuId的秒杀信息 */
    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTO seckillSkuRedisTO = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().put("data", seckillSkuRedisTO);
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId, @RequestParam("key") String key,
        @RequestParam("num") Integer num, Model model) {

        String orderSn = seckillService.seckill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
